package com.hns.can.mainte.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.hns.tool.pub.DateTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.mainte.entity.CommLogFileRcdEntity;
import com.hns.can.mainte.vo.LoadFileParamVo;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 下载文件service  (新增此service是为了新增事务生效而建立)
 *
 * @version Revision 2.0.0
 * @author:zhangzq
 * @email:
 */
@Service
public class LoadFileNService extends CommonService {

    /**
     * saveCommLogFileRcdEntity(新增文件写入日志记录)
     *
     * @param conditionVo
     * @param file        文件名
     * @author:zhangzq
     * @email:
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveCommLogFileRcdEntity(LoadFileParamVo conditionVo, String file) throws Exception {
        CommLogFileRcdEntity commLogFileRcdEntity = new CommLogFileRcdEntity();
        commLogFileRcdEntity.setRcrdId(UtilString.getUUID());
        commLogFileRcdEntity.setFailCount(new BigDecimal(0));
        commLogFileRcdEntity.setFileName(file);
        commLogFileRcdEntity.setFileSt("0");
        commLogFileRcdEntity.setFileType(conditionVo.getComponentType());

        //解析文件名，获取时间信息@xujf<

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //截取时间字符串
        int b1 = file.indexOf("-");
        int b2 = file.indexOf("_");
        String rstStr = file.substring(b1 + 1, b2);
        System.out.println(rstStr);

        //文件的开始时间
        String bstr = rstStr + "00";

        Date bdate = sdf1.parse(bstr);

//        System.out.println(bdate.toString());

        String bstr1 = sdf2.format(bdate);

        System.out.println("文件开始时间：" + bstr1);

        //文件的结束时间
        String estr = rstStr + "00";

        Date edate = sdf1.parse(estr);

        Date edate1 = DateTool.calculateByMinute(edate, 5);

//        System.out.println(edate1.toString());

        String estr1 = sdf2.format(edate1);

        System.out.println("文件结束时间：" + estr1);

        commLogFileRcdEntity.setFileBeginTime(bstr1);
        commLogFileRcdEntity.setFileEndTime(estr1);

        //解析文件名，获取时间信息>

        commLogFileRcdEntity.setGenTime(new Date());
        commonEntityDao.save(commLogFileRcdEntity);
        commonEntityDao.flush();
    }
    
    /**
     * @param commLogFileRcdEntity
     * @author:zhangzq
     * @email:
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateCommLogFileRcdEntity(CommLogFileRcdEntity commLogFileRcdEntity) {
        commonEntityDao.update(commLogFileRcdEntity);
        commonEntityDao.flush();
    }

    /**
     * 更新文件导入失败原因
     *
     * @param failReason 文件导入失败原因描述
     * @param file       文件名
     * @author:zhangzq
     * @email:
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateCommLogFileRcdEntityReason(String failReason, String file) {
        //查询出对应文件的记录
        List<CommLogFileRcdEntity> commLogFileRcdlist = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("loadFileMonitor.getLogFileRcd", file), CommLogFileRcdEntity.class);
        if (commLogFileRcdlist != null) {
            for (CommLogFileRcdEntity commLogFileRcdEntity : commLogFileRcdlist) {
                //更新成已导入状态
                commLogFileRcdEntity.setFailInfo(failReason);
                commonEntityDao.update(commLogFileRcdEntity);
            }
        }
        commonEntityDao.flush();
    }
}
